<?php

class BMessage extends BEntity {

    public $name = null;
    public $title;
    public $description;
    public $senderAddress;
    public $receiverAddress;
    public $sendTime;
    public $messageItems = array();
    protected $mediaManager = null;
    protected $messageManager = null;

    public function getPropertyToColumnMapList($data = NULL) {
        $mapList = parent::getPropertyToColumnMapList($data);
        $mapList['senderAddress'] = 'from_address';
        $mapList['receiverAddress'] = 'to_address';
        $mapList['sendTime'] = 'send_time';
        return $mapList;
    }

    public function addMessageItem($itemData, $data = NULL) {

        if (is_array($itemData)) {
            //get media type
            $mediaTypeId = $itemData['media_type'];
            $mediaType = new BMediaGroup($mediaTypeId);
            //create new message item
            $messageItem = new BMedia();
            $messageItem->name = $itemData['name'];
            $messageItem->content = $itemData['media_content'];
            $messageItem->save();
            //add messageItem type
            $mediaType->addMember($messageItem);
            //add messageItem into message
            $messagMediaModel = new BMMessageMedia();
            $messagMediaModel->message_id = $this->getId();
            $messagMediaModel->media_id = $messageItem->getId();
            $messagMediaModel->save();
        }
    }

    public function removeMessageItem($item) {
        
    }

    public function getMessageItem($itemContext = null, $data = NULL) {
        
    }

    public function getMessageItems($itemContext = NULL, $data = NULL) {
        
    }

    public function getMessageManager($data = NULL) {
        if ($this->messageManager == NULL)
            $this->messageManager = new BMessageManager($this);
        return $this->messageManager;
    }

    /**
     * 
     * @param type $data
     * @return BWMediaManager
     */
    public function getMediaManager($data = NULL) {
        if ($this->mediaManager == NULL) {
            $this->mediaManager = new BMediaManager($this);
        }
        return $this->mediaManager;
    }

    public function getSenderInfo() {
        $sender = new BAddress($this->senderAddress);
        $items = $sender->getAddressItemList();

        $result = '';
        $sep = '';
        foreach ($items as $item) {
            if ($item->userId > 0) {
                $user = new BUser($item->userId); //BUser::find($item->userId);
                if ($user != NULL) {
                    $result .= $sep . $user->getUserId();

                    $sep = ', ';
                }
            }
        }

        return $result;
    }

    public function save($context = null) {
        $messageTable = new BMessageSheet('message');

        $items = array();
        if ($this->senderAddress != NULL) {
            $items['senderAddressId'] = $this->senderAddress;
        }
        if ($this->receiverAddress != NULL) {
            $items['receiverAddressId'] = $this->receiverAddress;
        }
        if ($this->sendTime != NULL) {
            $items['sendTime'] = $this->sendTime;
        }
        if ($this->name != NULL) {
            $items['name'] = $this->name;
        }
        if ($this->title != NULL) {
            $items['title'] = $this->title;
        }
        if ($this->description != NULL) {
            $items['description'] = $this->description;
        }

        if (is_array($context))
            $items += $context;

        $list = $messageTable->addList($items);

        return $list->getId();
    }

}

?>
